//
//  OtherViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-10-15.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "OtherViewController.h"
#import "Macro.h"
#import "RegisterViewController.h"
#import "AppDelegate.h"
#import "HeaderView.h"
#import "NetServiseCommonData.h"
#import "OtherDetailViewController.h"
#import "MyOrderViewController.h"
#import "MyCollcetionViewController.h"
#import "MyScoreViewController.h"
#import "EveryDayResigterViewController.h"
#import "IniviteFriendViewController.h"
#import "SettingViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "ShoppingCartViewController.h"
#import "GroupPurchaseViewController.h"

@interface OtherViewController ()<UITableViewDataSource,UITableViewDelegate,headerViewDelegate>
{
    UITableView *otherTableView;
    HeaderView *headView;
}
@end

@implementation OtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isBackButton = NO;
    self.navigationItem.leftBarButtonItem = nil;
    
    [self setTitleView:@"我的"];
    otherTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64-40) style:UITableViewStyleGrouped];
    otherTableView.dataSource = self;
    otherTableView.delegate = self;
    otherTableView.scrollEnabled = NO;
    [self.view addSubview:otherTableView];
   // self.dataArray =  [[NetServiseCommonData getOtherData] objectForKey:@"list"];
    
    [self initHeadView];
    
    UIView *otherView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [self.view addSubview:otherView];
    otherView.userInteractionEnabled = YES;
    
//    UIButton *otherButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
//    [otherButton setImage:[UIImage imageNamed:@"add_weichat"] forState:UIControlStateNormal];
//    [otherButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
//    [otherButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [otherView addSubview:otherButton];
//    
//    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:otherView];
//    self.navigationItem.rightBarButtonItem = rightButtonItem;
  
//    self.textArray = @[@"我的订单",@"我的收藏",@"购物车",@"积分详情",@"邀请朋友",@"设置"];
//    self.imageArray = @[@"myorder",@"collect",@"purchase",@"ploderImage",@"invite_friend",@"setting"];
//    
    
    self.textArray = @[@"我的订单",@"团购订单",@"邀请朋友",@"设置"];
    self.imageArray = @[@"myorder",@"tuan",@"invite_friend",@"setting"];
    // Do any additional setup after loading the view.
}

#pragma mark shareAction
-(void)shareAction{
    
}

-(void)initHeadView{
    
    headView = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 120)];
    headView.delegate = self;
    [headView setOtherModel:self.otherModel];
    headView.backgroundColor = [UIColor whiteColor];
    
    if ([APP_DELEGATE isLogin]) {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeadViewAction:)];
        [headView addGestureRecognizer:tapGesture];
    }
   
    [otherTableView setTableHeaderView:headView];
}

-(void)joinLoginVC{
    
     [self LoginAciton];
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
     [MobClick endLogPageView:@"PageOne"];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getInfoData];
    [MobClick beginLogPageView:@"PageOne"];
}

#pragma mark getInfoData获取个人信息
-(void)getInfoData{
    
    __weak OtherViewController *weak_self = self;

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    SetValueForKey(params, [RegisterHelper shareHelper].userId, @"userId");
    
    
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kInfo
                             andHttpMethod:@"POST"
                             andParameters:params
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"responseJson:%@",request.responseJSON);
                             if([[request.responseJSON objectForKey:@"result"] intValue]==0){
                                 
                                 OtherModel *model = [[OtherModel alloc]initWithOtherInfo:[responseDic objectForKey:@"userInfo"]];
                                 [headView removeFromSuperview];
                                 self.otherModel = model;
                                 [self initHeadView];
                                 OrderModel *orderModel = [[OrderModel alloc]initWithOrderInfo:[responseDic objectForKey:@"orderStatist"]];
                                 self.orderModel = orderModel;
                             }
                             else{
                                 //[weak_self showToastMessage:@"获取用户信息失败,请注册登录!"];
                             }
                             
                         }

                     }];
}

#pragma mark tapAction
-(void)tapHeadViewAction:(UITapGestureRecognizer *)tap{
    NSLog(@"跳入OtherDetail");
    
    if (APP_DELEGATE.isLogin) {
        OtherDetailViewController *otherDetail = [[OtherDetailViewController alloc]init];
        otherDetail.otherModel = self.otherModel;
        [self.navigationController pushViewController:otherDetail animated:YES];
    }else{
        
        [self LoginAciton];
    }
}

#pragma  mark LoginAciton
-(void)LoginAciton{
    NSLog(@"进入登录");
    
    
    if (![APP_DELEGATE isLogin]) {
        [APP_DELEGATE pushLoginViewControllerWithStoryBoardIdentifier:@"LoginViewController" pushIn:self];
    }

}


#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _textArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, (50-22)/2, 22, 22)];
        imageView.tag = 100 + indexPath.row;
        [cell.contentView addSubview:imageView];
        
        UILabel *textLabel =[self createLabelWithFrame:CGRectMake(imageView.right+10, (50-25)/2, 120, 25) text:@"" font:14.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
        textLabel.tag = 1000 + indexPath.row;
        [cell.contentView addSubview:textLabel];
        
    }
    UIImageView *imageName = (UIImageView *)[cell.contentView viewWithTag: 100 + indexPath.row];
    imageName.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    
    UILabel *textLabel =(UILabel *)[cell.contentView viewWithTag:1000 + indexPath.row];
    textLabel.text = _textArray[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (APP_DELEGATE.isLogin == NO) {
        
        [self LoginAciton];
    }else{

            if(indexPath.row == 0)
            {
                //我的订单
                MyOrderViewController *orderVC = [[MyOrderViewController alloc]init];
                orderVC.hidesBottomBarWhenPushed = YES;
                orderVC.orderModel = self.orderModel;
                [self.navigationController pushViewController:orderVC animated:YES];
                
            }else if (indexPath.row == 1){
                //团购订单
                GroupPurchaseViewController *groupVC = [[GroupPurchaseViewController alloc]init];
                groupVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:groupVC animated:YES];
                
//            }else if(indexPath.row == 2)
//            {
//                ShoppingCartViewController *shoppingCart = [[ShoppingCartViewController alloc]init];
//                shoppingCart.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:shoppingCart animated:YES];
//                
//            }else if(indexPath.row == 3){
//                
//                //积分详情
//                MyScoreViewController *scoreVC = [[MyScoreViewController alloc]init];
//                scoreVC.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:scoreVC animated:YES];
//                
//            }
            }else if(indexPath.row == 2){
                
                //邀请朋友
                IniviteFriendViewController *iniviteFVC =[[NSBundle mainBundle]loadNibNamed:@"IniviteFriendViewController" owner:self options:nil][0];
                iniviteFVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:iniviteFVC animated:YES];
                
                
            }else if(indexPath.row == 3){
               //设置
                SettingViewController *settingVC = [[SettingViewController alloc]init];
                settingVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:settingVC animated:YES];
                
            }
    }
}

//-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
//    if(text == nil)
//        text = @"";
//    UILabel* label = [[UILabel alloc] initWithFrame:frame];
//    label.text = text;
//    label.font = [UIFont systemFontOfSize:font];
//    label.textAlignment = alig;
//    label.textColor = tc;
//    label.backgroundColor = [UIColor clearColor];
//    return label;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
